import { currentRole, role1, role2 } from "../index.js";
import { scoket } from "../utils/scoket.js";
import { removeKey, setKey } from "./scheduler.js";
export const getPosition = (ele) => {
    const po = ele.style.transform;
    const xy = po.slice(po.indexOf("(") + 1, po.indexOf(")")).split(",");
    return {
        x: +xy[0].replace("px", ""),
        y: +xy[1].replace("px", ""),
    };
};
//按键触发移动
let keyword = ["a", "s", "d", "w", "j"];
let lastKey = "";
export function transfrom(e) {
    if (!keyword.includes(e.key))
        return;
    if (lastKey === e.key)
        return;
    lastKey = e.key;
    setKey(e.key);
}
scoket.$on((res) => {
    if (res.path !== "/key/down")
        return;
    if (res.role === "p1") {
        role1.style.transform = setTransfrom(res.value.rolePosition);
    }
    if (res.role === "p2") {
        role2.style.transform = setTransfrom(res.value.rolePosition);
    }
});
//停止移动
export function stopTransform(e) {
    if (!keyword.includes(e.key))
        return;
    lastKey = "";
    removeKey(e.key);
}
export const getRolePosition = () => {
    return getPosition(currentRole);
};
export const setTransfrom = (po) => {
    return `translate(${po.x}px, ${po.y}px)`;
};
